//
//  ClassifyItemView.m
//  B2C
//
//  Created by xiaowu on 2017/8/3.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "ClassifyItemView.h"

static CGFloat iconOffset = 18;

@interface ClassifyItemView ()
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *button;
@end

@implementation ClassifyItemView

+ (CGFloat)getHeightWithWidth:(CGFloat)width {
    CGFloat height = 0;
    height += width - iconOffset;
    height += 37;
    return height;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.iconImageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.button];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.iconImageView.frame = CGRectMake(iconOffset, iconOffset, self.width - iconOffset * 2, self.width - iconOffset * 2);
    self.titleLabel.frame = CGRectMake(0, self.height - 37, self.width, 37);
    self.button.frame = self.bounds;
}

- (void)setItemViewWithImageUrl:(NSString *)url title:(NSString *)title {
    
    //[self.iconImageView animationWithImageURL:url placeholderImage:PlaceholderImageSmall];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
    
    self.titleLabel.text = title;
}


#pragma mark - Action
- (void)buttonClick:(UIButton *)sender {
    if (self.block) {
        self.block();
    }
}

#pragma mark - Getters
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        [_iconImageView setBackgroundColor:[UIColor imageEDBackgroundColor]];
        [_iconImageView setContentMode:UIViewContentModeCenter];
    }
    return _iconImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = TextPingFangFontSize(12);
        _titleLabel.textColor = [UIColor gray66Color];
    }
    return _titleLabel;
}

- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}




@end
